package cn.edu.zucc.controller;

import cn.edu.zucc.entity.Article;
import cn.edu.zucc.entity.Stu;
import cn.edu.zucc.entity.User;
import cn.edu.zucc.repository.StuRepository;
import cn.edu.zucc.service.StuService;
import cn.edu.zucc.util.PageUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


//@Controller
////@RequestMapping
//@RequestMapping("stu")
public class StuController {

//    @GetMapping("/listAdmin")
//    @GetMapping("/stu")
//    @GetMapping("/list")
//    public ModelAndView stu(@RequestParam(value = "pageNum", defaultValue = "0")
//                                        int pageNum) {
//
////        Map user= new HashMap();
////        user.put("name", "姓名");
////        user.put("sex", "male");
////
////        ModelAndView modelAndView = new ModelAndView("map");
////        modelAndView.addObject("map", user);
////
//        System.out.println("pageNum");
//        System.out.println(pageNum);
//
//        Map<String,Integer> pageNumMap= new HashMap<>();
//        pageNumMap.put("currentPage",1);
//        pageNumMap.put("startPage",1);
//        pageNumMap.put("currentPage",1);
////        PageUtil.
////        pageNum.put("currentPage",1);
//        ModelAndView modelAndView = new ModelAndView("stu");
//        modelAndView.addObject("pageNum", pageNumMap);
//
//
//        return modelAndView;
//    }

    @Resource
    StuRepository stuRepository;
//    StuRe
//    但是这是 thymeleaf 。。 应该不能reactive吧。。
    @GetMapping("/list")
    public ModelAndView stu(@RequestParam(value = "start", defaultValue = "0") Integer start,
                            @RequestParam(value = "limit", defaultValue = "5") Integer limit) {
        start = start < 0 ? 0 : start;
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(start, limit, sort);
//        stu
        Page<Stu> page = stuRepository.findAll(pageable);
        ModelAndView mav = new ModelAndView("stu/list");
        mav.addObject("page", page);
        return mav;
    }

    @Resource
    StuService stuService;

    @GetMapping("/form")
    public String form(ModelMap map) {
//        model 是后端造出来的
        Stu user = new Stu();
        user.setName("test_ps");
        user.setSex("男的");

        map.put("stu", user);
        return "stu/form";
    }

//    https://blog.csdn.net/yelllowcong/article/details/79830468
    @PostMapping("/add")
    public ModelAndView add(@ModelAttribute Stu stu) {
        String name = stu.getName();
        System.out.println("name");
        System.out.println(name);
        stuRepository.save(stu);
        int start=0;
        int limit=5;
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(start, limit, sort);
//        stu
        Page<Stu> page = stuRepository.findAll(pageable);
        ModelAndView mav = new ModelAndView("stu/list");
        mav.addObject("page", page);
        return mav;
    }
}
